package org.timemanager.view;

import java.util.Calendar;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * show category statistic chart
 * @author Dereky
 *
 */
public class CategoryAnalyzeActivity extends Activity {
	/**
	 * record current time.
	 */
	public static Calendar curCalendar;
	/**
	 * store while category to be drawn.
	 */
	public static int categoryId;
	private TextView categoryAnalyze;
	private CategoryAnalyzeView categoryCanvas;
	/**
	 * true means drawing bar statistics chart,
	 * false means drawing pie statistics chart.
	 */
	public static boolean isShowBarView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		curCalendar=Calendar.getInstance();
		curCalendar.set(Calendar.SECOND, 0);
		curCalendar.set(Calendar.MILLISECOND, 0);
		categoryId=getIntent().getIntExtra("categoryId", -1);
		setContentView(R.layout.category_analyze);
		initAllConfiguration();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		categoryCanvas.close();
	}

	/**
	 * initialize all parameters
	 */
	private void initAllConfiguration(){
		
		findViewById(R.id.category_return).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				finish();
			}
		});
		categoryAnalyze= (TextView) findViewById(R.id.category_analyze);
		categoryCanvas=(CategoryAnalyzeView) findViewById(R.id.category_canvas);
		String analyzeTitle="\n"+getText(R.string.category_analyzez_title).toString();
		/*
		 * obtain the category name due to category id,
		 * id equal to -2 means all categories,-1 means unclassified category,
		 * these two kind is not stored in the database,
		 * so list them alone 
		 */
		if(categoryId==-2){
			analyzeTitle=getText(R.string.all_category).toString()+analyzeTitle;
		}
		else if(categoryId==-1){
			analyzeTitle=getText(R.string.unclassified_category).toString()+analyzeTitle;
		}
		else{
			DatabaseTool databaseTool=new DatabaseTool(CategoryAnalyzeActivity.this);
			Cursor cursor=databaseTool.getCategory(categoryId);
			cursor.moveToFirst();
			analyzeTitle=cursor.getString(1)+analyzeTitle;
			try{
				//cursor.close();
				databaseTool.close();
			}catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		((TextView) findViewById(R.id.category_title)).setText(analyzeTitle);
		
		isShowBarView=true;
		categoryAnalyze.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				isShowBarView=!isShowBarView;
				showStatisticView();
			}
		});
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		showStatisticView();
	}
	/**
	 * show bar statistics chart or pie statistics chart.
	 */
	private void showStatisticView(){
		if(isShowBarView){
			categoryCanvas.showBarView();
			categoryAnalyze.setText(getText(R.string.category_analyze_pie));
			
		}
		else{
			categoryCanvas.showPieView();
			categoryAnalyze.setText(getText(R.string.category_analyze_bar));
		}
	}
}
